package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Ckqbg;
import vip.xiaonuo.smzq.modular.kcl.entity.Ckqsqdjs;
import vip.xiaonuo.smzq.modular.kcl.entity.Splc;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.SplcVo;
import vip.xiaonuo.smzq.modular.kcl.mapper.CkqbgMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.CkqsqdjsMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.SplcMapper;
import vip.xiaonuo.smzq.modular.kcl.service.ISplcService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.lifecycle.po.SavePedigeePO;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.LifeCycleHanderDataService;
import vip.xiaonuo.smzq.modular.user.entity.Org;
import vip.xiaonuo.smzq.modular.user.entity.Position;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.OrgMapper;
import vip.xiaonuo.smzq.modular.user.mapper.PositionMapper;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 矿山审批流程 服务实现类
 *
 * @author sqq
 * @since 2024-04-22
 */
@Service
public class SplcServiceImpl extends ServiceImpl<SplcMapper, Splc> implements ISplcService {

    @Resource
    private SplcMapper splcMapper;
    @Resource
    private OrgMapper orgMapper;//级别部省市县填报
    @Resource
    private PositionMapper positionMapper;//所对应的行政区划
    @Resource
    private UserMapper userMapper;//用户
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划
    @Resource
    private CkqsqdjsMapper ckqsqdjsMapper;
    @Autowired
    LifeCycleHanderDataService lifeCycleHanderDataService;
    @Resource
    private CkqbgMapper ckqbgMapper;

    @Override
    public Page<Splc> page(Splc splc) {
        QueryWrapper<Splc> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public void addSplcVo(SplcVo splcVo) {
        Splc splc = new Splc();
        splc.setGlzd(splcVo.getSqdjsId());
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        //县
        String xzqhdm = user.getDirectorId();
        if (xzqhdm != null) {
            splc.setXian(xzqhdm);
            //市
            QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
            shiWrapper.eq("dm", Integer.parseInt(xzqhdm));
            Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
            splc.setShi(String.valueOf(shi.getFdm()));
            //省
            QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
            shengWrapper.eq("dm", shi.getFdm());
            Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
            splc.setSheng(String.valueOf(sheng.getFdm()));
            //矿山名称
            splc.setKsmc(splcVo.getKsmc());
            //许可证号
            splc.setXkzh(splcVo.getXkzh());
            //矿权人
            splc.setKyqr(splcVo.getKyqr());
            //登记类型
            splc.setDjlx(splcVo.getDjlx());
            //审批状态
            splc.setSpzt("0");
            //创建人
            splc.setCreatBy(saBaseLoginUser.getId());
            //创建时间
            splc.setCteatTime(DateUtil.format(new Date(), "YYYYMMdd"));
            splcMapper.insert(splc);
        }
    }

    @Override
    public Page<SplcVo> pageSplcVo(long current, long size, Integer nd, String xzqhdm, Integer djlx) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Org org = orgMapper.selectById(user.getOrgId());
        String code = org.getCode();
        Position position = positionMapper.selectById(user.getPositionId());
        String positionCode = position.getCode();


        Page<Splc> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Splc> splcList = new ArrayList<>();
        List<SplcVo> splcVoList = new ArrayList<>();
        Page<SplcVo> splcVoPage = new Page<>();
        QueryWrapper<Splc> queryWrapper = new QueryWrapper<>();


//        if ( positionCode.equals("zrzyb")) {
//            queryWrapper.eq("spzt", "3");
//        } else {
//            positionCode = positionCode.replaceAll("0+$", "");
//            if (positionCode.length() == 6) {
//                queryWrapper.eq("spzt", "0");
//            } else if (positionCode.length() == 4) {
//                queryWrapper.eq("spzt", "1");
//            } else if (positionCode.length() == 2) {
//                queryWrapper.eq("spzt", "2");
//            }


        // queryWrapper.like("sheng", positionCode).like("shi", positionCode).like("xian", positionCode);
//      }
        if (xzqhdm != null) {
            xzqhdm = xzqhdm.replaceAll("0+$", "");
            queryWrapper.like("sheng", xzqhdm).like("shi", xzqhdm).like("xian", xzqhdm);
        }
        if (djlx == 2) {
            queryWrapper.eq("djlx", "变更");
        }

        if (code.equals("shengsh")) {
            queryWrapper.eq("spzt", "2").or().eq("spzt", "3").or().eq("spzt", "5");
        } else if (code.equals("shish")) {
            queryWrapper.eq("spzt", "1").or().eq("spzt", "2").or().eq("spzt", "3").or().eq("spzt", "5");
        } else if (code.equals("xiansh")) {
            queryWrapper.eq("spzt", "0").or().eq("spzt", "1").or().eq("spzt", "2").or().eq("spzt", "3").or().eq("spzt", "5");
        } else if (code.equals("bjsh")) {
            queryWrapper.eq("spzt", "3").or().eq("spzt", "5");
        } else if (code.equals("zt7qwacif0")) {
            queryWrapper.eq("creat_by", saBaseLoginUser.getId());
        }
        queryWrapper.orderByAsc("spzt");
        queryWrapper.orderByDesc("cteat_time");
//        else{
//            queryWrapper.ne("spzt","0");
//        }

        page = splcMapper.selectPage(page, queryWrapper);

        splcList = page.getRecords();

        for (Splc splc : splcList) {
            SplcVo splcVo = new SplcVo();
            BeanUtil.copyProperties(splc, splcVo);
            //县
            QueryWrapper<Cb08Xzqh> xianWrapper = new QueryWrapper<>();
            xianWrapper.eq("dm", Integer.parseInt(splc.getXian()));
            Cb08Xzqh xian = cb08XzqhMapper.selectOne(xianWrapper);
            splcVo.setXianMc(xian.getMc());
            //市
            QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
            shiWrapper.eq("dm", Integer.parseInt(splc.getShi()));
            Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
            splcVo.setShiMc(shi.getMc());
            //省
            QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
            shengWrapper.eq("dm", Integer.parseInt(splc.getSheng()));
            Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
            splcVo.setShengMc(sheng.getMc());

            splcVo.setKqrmc(saBaseLoginUser.getName());
            splcVo.setDjlxMc("变更");
            splcVo.setSqdjsId(splc.getGlzd());

            int approvalStatus = Integer.parseInt(splc.getSpzt());
            if (approvalStatus == 0) {
                // 未审批
                splcVo.setSpztMc("未审批");
            } else if (approvalStatus == 1) {
                // 县审批
                splcVo.setSpztMc("县审批");
            } else if (approvalStatus == 2) {
                // 市审批
                splcVo.setSpztMc("市审批");
            } else if (approvalStatus == 3) {
                // 省审批
                splcVo.setSpztMc("省审批");
            } else if (approvalStatus == 4) {
                // 部审批
                splcVo.setSpztMc("部审批");
            } else if (approvalStatus == 5) {
                // 审批完成
                splcVo.setSpztMc("审批完成");
            }
            splcVoList.add(splcVo);
        }

        splcVoPage.setCurrent(page.getCurrent());
        splcVoPage.setSize(page.getSize());
        splcVoPage.setRecords(splcVoList);
        splcVoPage.setTotal(page.getTotal());

        return splcVoPage;
    }

    @Override
    public void updateSplc(Splc splc) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Org org = orgMapper.selectById(user.getOrgId());
        splc.setSpjg("0");
        if (splc.getSpjg().equals("0")) {
            String parentid = org.getParentId();
            String code = org.getCode();
            QueryWrapper<Position> positionWrapper = new QueryWrapper<>();
            if (parentid.equals("0")) {
                positionWrapper.eq("org_id", "1763387811067281409");
            } else if (!parentid.equals("1")) {
                positionWrapper.eq("org_id", parentid);
                if (code.equals("shengsh")) {
                    positionWrapper.eq("code", "zrzyb");
                } else if (code.equals("shish")) {
                    positionWrapper.eq("code", splc.getSheng());
                } else if (code.equals("xiansh")) {
                    positionWrapper.eq("code", splc.getShi());
//                    SavePedigeePO savePedigeePO = new SavePedigeePO();
//                    savePedigeePO.setId("C"+DateUtil.format(new Date(), "YYYYMMddHHmmss"));
//                    savePedigeePO.setParentId(splc.getXkzh());
//                    savePedigeePO.setYear(Integer.valueOf(DateUtil.format(new Date(), "YYYY")));
//                    lifeCycleHanderDataService.savePedigeeWhenApproveed(savePedigeePO);

                    SavePedigeePO savePedigeePO = new SavePedigeePO();
                    savePedigeePO.setYear(Integer.valueOf(DateUtil.format(new Date(), "YYYY")));
                    savePedigeePO.setId("C" + DateUtil.format(new Date(), "YYYYMMddHHmmss"));
                    savePedigeePO.setParentId(splc.getXkzh());
                    savePedigeePO.setReportCode(300006);
                    savePedigeePO.setExportType(301003);
                    savePedigeePO.setStoreDate(DateUtil.format(new Date(), "YYYYMMdd"));
                    savePedigeePO.setContinueDate(Convert.convert(LocalDate.class, DateUtil.current()));
                    savePedigeePO.setApproveStatus(1);
                    Ckqsqdjs ckqsqdjs = ckqsqdjsMapper.selectById(splc.getGlzd());
                    if (ckqsqdjs.getCkqbhlx() == 1) {
                        QueryWrapper<Ckqbg> ckqbgWrapper = new QueryWrapper<>();
                        ckqbgWrapper.eq("ckqsqid", splc.getGlzd());
                        Ckqbg ckqbg = ckqbgMapper.selectOne(ckqbgWrapper);
                        savePedigeePO.setFileId(ckqbg.getPkid());
                    }
                    boolean result = lifeCycleHanderDataService.savePedigeeWhenApproveed(savePedigeePO);

                } else if (code.equals("zt7qwacif0")) {
                    positionWrapper.eq("code", splc.getXian());
                }
            }
            Position position = positionMapper.selectOne(positionWrapper);
//            QueryWrapper<User> userWrapper=new QueryWrapper<>();
//            userWrapper.eq("position_id",position.getId());
//            User useroN = userMapper.selectOne(userWrapper);
            String positionCode = position.getCode();
            positionCode = positionCode.replaceAll("0+$", "");
            if (positionCode.length() == 6) {
                splc.setSpzt("0");
            } else if (positionCode.length() == 4) {
                splc.setSpzt("1");
            } else if (positionCode.length() == 2) {
                splc.setSpzt("2");
            } else if (positionCode.equals("zrzyb")) {
                splc.setSpzt("3");

                if (parentid.equals("0")) {
                    splc.setSpzt("5");
                }
            }

        }
        splcMapper.updateById(splc);
    }
}
